//
//  HMShoppingCarTableViewController.m
//  HowToDo
//
//  Created by 宋轩 on 17/1/6.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMShoppingCarTableViewController.h"
#import "HMShoppingCarTableViewCell.h"
#import "HMShoppingCarModel.h"
#import "NSString+HMTimeString.h"
#import "HMPayViewController.h"
#import "UIViewController+Extension.h"


@interface HMShoppingCarTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSArray *shoppingCarData;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, assign) NSInteger selectBtnNum;
@property (nonatomic, assign) CGFloat allPrice;
@property (nonatomic, strong) UILabel *priceLabel;

@end

static NSString *shoppingCellID = @"shoppingCellID";

@implementation HMShoppingCarTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"购物车";
    
    self.selectBtnNum = 0;
    self.allPrice = 0;
    
    UITableView *shoppingCarTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    shoppingCarTableView.delegate = self;
    
    shoppingCarTableView.dataSource = self;
    
    [self.view addSubview:shoppingCarTableView];
    
    [shoppingCarTableView registerNib:[UINib nibWithNibName:@"HMShoppingCarTableViewCell" bundle:nil] forCellReuseIdentifier:shoppingCellID];
    
    //解档
    NSString *cachesStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *fileName = [cachesStr stringByAppendingPathComponent:@"content.plist"];
    
    self.shoppingCarData = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
    
    [shoppingCarTableView reloadData];
    
    if (self.shoppingCarData.count == 0)
    {
        [self showTipViewWithName:@"empty_placeholder_90x90_" andMessage:@"没有商品"];
        return;
    }
    
    [self createSettleView];
}

- (NSArray *)btnArray
{
    if (_btnArray == nil)
    {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//创建底部视图
- (void)createSettleView
{
    UIView *settleView = [[UIView alloc] init];
    settleView.userInteractionEnabled = YES;
    [self.view addSubview:settleView];
    settleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [settleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(44);
    }];
    
    UIButton *selectBtn = [[UIButton alloc] init];
    
    self.selectBtn = selectBtn;
    
    [settleView addSubview:selectBtn];
    [selectBtn setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"checkSelected"] forState:UIControlStateSelected];
    
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(settleView.mas_left).offset(20);
        make.centerY.equalTo(settleView);
        make.width.height.offset(20);
    }];
    [selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *priceLabel = [UILabel hm_labelWithText:@"合计: ￥0.00" fontSize:15 color:[UIColor orangeColor]];
    self.priceLabel = priceLabel;
    [settleView addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(selectBtn.mas_right).offset(20);
        make.centerY.equalTo(settleView);
    }];
    
    UIButton *settleBtn = [[UIButton alloc] init];
    [settleView addSubview:settleBtn];
    [settleBtn setTitle:@"结算" forState:UIControlStateNormal];
    settleBtn.backgroundColor= [UIColor orangeColor];
    
    [settleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(settleView);
        make.right.offset(-20);
        make.height.offset(34);
        make.width.offset(100);
    }];
    
    [settleBtn addTarget:self action:@selector(settleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)settleBtnClick:(UIButton *)btn
{
//    HMPayViewController *vc = [[HMPayViewController alloc] init];
//    
//    [self dismissViewControllerAnimated:YES completion:nil];
//    
//    [self.navigationController pushViewController:vc animated:YES];
    [self showHudAuto:@"余额不足,请充值"];
}

//数据源方法
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.shoppingCarData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMShoppingCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:shoppingCellID forIndexPath:indexPath];
    [cell.coverPhotoImageView sd_setImageWithURL:[NSURL URLWithString:[self.shoppingCarData[indexPath.row] valueForKey:@"image_url"]]placeholderImage:[UIImage imageNamed:@"placeHolder"]];
    
    cell.resourceNameLabel.text = [self.shoppingCarData[indexPath.row] valueForKey:@"resource_name"];
    
    cell.resourceContentLabel.text = [self.shoppingCarData[indexPath.row] valueForKey:@"resource_content"];
    
    cell.resourceEnclosureLabel.text = [NSString getTimeWithString:[self.shoppingCarData[indexPath.row] valueForKey:@"resource_enclosure"]];
    
    cell.priceLabel.text = [NSString stringWithFormat:@"¥ : %@", [self.shoppingCarData[indexPath.row] valueForKey:@"price"]];

    [self.btnArray addObject:cell.selectBtn];
    
    cell.selectBtn.tag = indexPath.row;
    
    [cell.selectBtn addTarget:self action:@selector(selectBtnClick1:) forControlEvents:UIControlEventTouchUpInside];
    
//    self.priceLabel.text = [NSString stringWithFormat:@"%f", self.allPrice];
    
    return cell;
}

- (void)selectBtnClick1:(UIButton *)btn
{
    if (btn.selected == YES )
    {
        btn.selected = NO;
        self.selectBtnNum --;
        if (self.selectBtnNum == 0)
        {
            self.selectBtn.selected = NO;
        }
        self.allPrice -= [self.shoppingCarData[btn.tag] price].floatValue;
        
    }
    else
    {
        btn.selected = YES;
        self.selectBtnNum++;
        self.selectBtn.selected = YES;
        self.allPrice += [self.shoppingCarData[btn.tag] price].floatValue;
      
    }
      self.priceLabel.text = [NSString stringWithFormat:@"合计: ￥%.2f", self.allPrice];
}

- (void)selectBtnClick:(UIButton *)btn
{
    if (btn.selected)
    {
        btn.selected = NO;
        self.selectBtnNum = 0;
        for (NSInteger i = 0; i < self.btnArray.count; i++)
        {
            UIButton *cellBtn = self.btnArray[i];
            cellBtn.selected = NO;
            self.allPrice -= [[self.shoppingCarData[i] valueForKey:@"price"] floatValue];
        }
        
    }
    else
    {
        btn.selected = YES;
        self.selectBtnNum = self.btnArray.count;
        for (NSInteger i = 0; i < self.btnArray.count; i++)
        {
            UIButton *cellBtn = self.btnArray[i];
            cellBtn.selected = YES;
            self.allPrice += [[self.shoppingCarData[i] valueForKey:@"price"] floatValue];
        }
    }
     self.priceLabel.text = [NSString stringWithFormat:@"合计: ￥%.2f", self.allPrice];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115;
}
@end
